<?php
class Api_Lib_Rsp_Client {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Jichu_Client');
        $this->_modelEmploy = FLEA::getSingleton('Model_Jichu_Employ');
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function updateClient($params,& $service){
        if(!$params['compName']){
            $service->send_user_error('compName required');
        }
        if(!$params['linkId']){
            $service->send_user_error('linkId required');
        }

        $params['compCode'] = strval($params['compCode']);
        //检查是否存在
        $client = $this->_modelExample->find(array('linkId'=>$params['linkId']));
        $employ = $this->_modelEmploy->find(array('linkId'=>$params['traderId']));

        $_data = array(
            'id'        => $client['id']+0,
            'compCode'  => $params['compCode'],
            'compName'  => $params['compName'],
            'traderId'  => $employ['id']+0,
            'people'    => $params['people'],
            'tel'       => $params['tel'],
            'mobile'    => $params['mobile'],
            'fax'       => strval($params['fax']),
            'bank'      => strval($params['bank']),
            'accountId' => strval($params['accountId']),
            'taxId'     => strval($params['taxId']),
            'address'   => strval($params['address']),
            'email'     => strval($params['email']),
            'memo'      => strval($params['memo']),
            'linkId'    => strval($params['linkId']),
        );

        //数据保存
        $result = $this->_modelExample->save($_data);
        $client['id'] = $client['id'] ? $client['id'] : $result;

        return array('success'=>$result ? true :false ,'msg'=>'完成','data'=>array('id'=>$client['id']));
    }

    /**
     *
     * Time：2019/11/07 14:01:47
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function delete($params,& $service){
        if(!$params['linkId']){
            $service->send_user_error('linkId required');
        }

        $_modelChuku= FLEA::getSingleton('Model_Sample_Chuku');
        $params['linkId'] = strval($params['linkId']);

        //查找是否被使用，如果被使用则禁止删除
        $client = $this->_modelExample->find(array('linkId'=>$params['linkId']));
        //查找是否有用到
        $count = $_modelChuku->findCount(array('clientId'=>$client['id']));

        if($count > 0){
            $res = false;
        }else{
            $res = $this->_modelExample->removeByConditions(array('linkId'=>$params['linkId']));
        }

        return array('success'=>$res ,'msg'=>'完成');
    }

    //批量操作
    function updateLot($params,& $service){
        $list = $params['list'];
        if(!$params['list']){
            $service->send_user_error('param list required');
        }
        //循环保存
        $error = array();
        $success = array();
        foreach ($list as $key => & $v) {
            if(!$v['compName']){
                $error[] = $key;
                continue;
            }

            $res = $this->updateClient($v);
            if($res['success']){
                $success[] = $key;
            }else{
                $error[] = $key;
            }
        }

        //返回数据的key键值数组
        return array('result'=>array('success'=>$success,'error'=>$error));
    }


    /**
     * 获取客户列表
     * Time：2019/11/07 15:09:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $condition = array();
        if($params['compName']!='') {
            $condition[] = array('compName',"%{$params['compName']}%",'like');
        }
        if($params['key']!='') {
            $condition[] = array('compName',"%{$params['key']}%",'like');
        }
        if($params['compCode']!='') {
            $condition[] = array('compCode',"%{$params['compCode']}%",'like');
        }

        // $this->_modelExample->clearLinks();
        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id desc',$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        //把需要的数据带出来
        $rows = array();
        foreach ($rowset as $key => & $v) {
            $rows[] = array(
                'cid'        => $v['id'],
                'compCode'   => $v['compCode'],
                'compName'   => $v['compName'],
                'people'     => $v['people'],
                'tel'        => $v['tel'],
                'mobile'     => $v['mobile'],
                'fax'        => $v['fax'],
                'bank'       => $v['bank'],
                'accountId'  => $v['accountId'],
                'taxId'      => $v['taxId'],
                'address'    => $v['address'],
                'email'      => $v['email'],
                'memo'       => $v['memo'],
                'linkId'     => $v['linkId'],
                'traderName' => strval($v['Trader']['employName']),
            );
        }

        return array('row'=>$rows,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rows));
    }
}
?>